//
//  NMPOVpnResultViewController.swift
//  NMPO
//
//  Created by PowerfullClean on 2023/10/31.
//

import UIKit

class NMPOVpnResultViewController: NMPOResultViewController {
    init(nmpo_connected: Bool = true) {
        super.init(nibName: nil, bundle: nil)
        self.nmpo_connected = nmpo_connected
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !nmpo_connected {
            nmpo_navView.nmpo_titleLabel.text = "DISCONNECTED"
        }
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(nmpo_enterActive),
            name: UIApplication.didBecomeActiveNotification, object: nil)
    }
    // MARK: - nmpo action
    override func nmpo_layout() -> UICollectionViewFlowLayout {
        let nmpo_layout = UICollectionViewFlowLayout()
        nmpo_layout.minimumLineSpacing = 0
        nmpo_layout.minimumInteritemSpacing = 0
        return nmpo_layout
    }
    override func nmpo_collectionViews() {
        super.nmpo_collectionViews()
        view.addSubview(nmpo_navView)
        nmpo_collectionView.snp.makeConstraints { make in
            make.bottom.left.width.equalToSuperview()
            make.top.equalTo(nmpo_navView.snp.bottom)
        }
        nmpo_collectionView.register(NMPOVpnResultCell.classForCoder(), forCellWithReuseIdentifier: NMPOVpnResultCell.nmpo_viewToString)
        nmpo_collectionView.register(NMPOVpnResultItemCell.classForCoder(), forCellWithReuseIdentifier: NMPOVpnResultItemCell.nmpo_viewToString)
        nmpo_collectionView.dataSource = self
        nmpo_collectionView.delegate = self
        nmpo_collectionView.contentInset = UIEdgeInsets(top: 0, left: 15, bottom: NMPOCommon.nmpo_XBottomSpace+15, right: 15)
    }
    @objc func nmpo_enterActive() {
        if nmpo_connected && (NMPOManager.nmpo_manager.nmpo_vpnCurStateType == .nmpo_invalid || NMPOManager.nmpo_manager.nmpo_vpnCurStateType == .nmpo_disconnected) {
            nmpo_navView.nmpo_titleLabel.text = "DISCONNECTED"
            nmpo_connected = false
            nmpo_collectionView.reloadData()
        }
    }
    // MARK: - nmpo lazy
    var nmpo_connected = true
    override func nmpo_baseBgName() -> String? {
        return "nmpo_list_bg"
    }
    lazy var nmpo_navView: NMPOServersListNavView = {
        let nmpo_navView = NMPOServersListNavView()
        nmpo_navView.nmpo_titleLabel.text = "CONNECTED"
        nmpo_navView.nmpo_leftBlock = {[weak self] in
            self?.navigationController?.popToRootViewController(animated: true)
        }
        return nmpo_navView
    }()
    lazy var nmpo_funcModels: [NMPOFuncModel] = {
        var nmpo_funcModels = [NMPOFuncModel]()
        nmpo_funcModels.append(NMPOFuncModel(nmpo_type: .nmpo_speed,nmpo_title: "Speed test", nmpo_icon: "nmpo_result_connectSpeed"))
        nmpo_funcModels.append(NMPOFuncModel(nmpo_type: .nmpo_ip,nmpo_title: "IP information", nmpo_icon: "nmpo_result_connectIP"))
        return nmpo_funcModels
    }()
    
}
extension NMPOVpnResultViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 1 {
            return nmpo_funcModels.count
        }
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 1 {
            let nmpo_cell = collectionView.dequeueReusableCell(withReuseIdentifier: NMPOVpnResultItemCell.nmpo_viewToString, for: indexPath) as! NMPOVpnResultItemCell
            nmpo_cell.nmpo_funcModel = nmpo_funcModels[indexPath.row]
            return nmpo_cell
        }
        let nmpo_cell = collectionView.dequeueReusableCell(withReuseIdentifier: NMPOVpnResultCell.nmpo_viewToString, for: indexPath) as! NMPOVpnResultCell
        if !nmpo_connected {
            nmpo_cell.nmpo_successLabel.text = "Successfully disconnected to VPN\nwelcome to VPN again"
            nmpo_cell.nmpo_iconView.image = UIImage(named: "nmpo_result_disConnectIcon")
            nmpo_cell.nmpo_timeLabel.text = "-:-:-"
            nmpo_cell.nmpo_titleLabel.text = ""
            nmpo_cell.nmpo_stopConnectTimer()
        }else {
            nmpo_cell.nmpo_serverModel = NMPOManager.nmpo_manager.nmpo_curServerModel
        }
        return nmpo_cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 1 {
            return CGSize(width: floor((nmpo_SCREEN_WIDTH-30)/2.0), height: 173)
        }
        return CGSize(width: nmpo_SCREEN_WIDTH-30, height: 243+8+68)
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            return
        }
        if NMPOManager.nmpo_manager.nmpo_netType == .nmpo_none {
            NMPOHudVender.nmpo_showMessage("No available network", isError: false)
            return
        }
        let nmpo_funcModel = nmpo_funcModels[indexPath.row]
        navigationController?.pushViewController(NMPOScanViewController(nmpo_funcType: nmpo_funcModel.nmpo_type), animated: true)
    }
}


